package com.iasku.iaskueleclass_phone.activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.iasku.constant.Constant;
import com.iasku.constant.IntentConfig;
import com.iasku.iaskueleclass_phone.R;
import com.iasku.iaskueleclass_phone.ReturnData;
import com.iasku.iaskueleclass_phone.Status;
import com.iasku.manager.DataManager;
import com.iasku.manager.IDataManager;

public class HomeworkSubmitAnswerActivity extends BaseActivity {
    private static final int TASK_GET_PIC = 1;
    
    private ImageView mImgv;
    private ImageButton mPrevBtn,mNextBtn;
    private Bitmap[] mBitmaps;
    private String mAnswerid;
    private int mPageno;
    private Bitmap mCurBitmap;
    
    private void setupView(){
        mImgv = (ImageView) findViewById(R.id.iv_answer_img);
        mPrevBtn = (ImageButton) findViewById(R.id.iv_answer_previous);
        mNextBtn = (ImageButton) findViewById(R.id.iv_answer_next);
    }
    
    private void addListener(){
        MyOnClickListener mListener = new MyOnClickListener();
        mPrevBtn.setOnClickListener(mListener);
        mNextBtn.setOnClickListener(mListener);
    }
    
    private void initView(){
        setupView();
        addListener();
        String attach = getIntent().getStringExtra(IntentConfig.ANSWER_ATTACH);
        mPageno = 0;
        try {
            JSONArray mJSONArray = new JSONArray(attach);
            mBitmaps = new Bitmap[mJSONArray.length()];
            JSONObject mJSONObject  = mJSONArray.getJSONObject(0);
            mAnswerid = mJSONObject.optString("answerid");
            mBarCenterTitleTv.setText("第" + (mPageno + 1) + "页，共" + mBitmaps.length + "页");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        setButtonStatus();
        if(mBitmaps[mPageno] == null){
            startTask(TASK_GET_PIC);
        }else{
            mImgv.setImageBitmap(mBitmaps[mPageno]);
        }
        
    }
    
    @Override
    protected void onCreate(Bundle saveInstance) {
        // TODO Auto-generated method stub
        super.onCreate(saveInstance);
        setContentView(R.layout.homework_answer_submitorcorrected);
        initTitleBar();
        initView();
    }
    
    @Override
    public int startTask(int taskId) {
        if(taskId == TASK_GET_PIC){
            mProgressDialog.setMessage("正在加载...");
            mProgressDialog.show();
        }
        return super.startTask(taskId);
    }
    
    @Override
    public Status onTaskInBackground(int taskId, Bundle bundle) {
        switch (taskId) {
        case TASK_GET_PIC:{
            String loadPath = Constant.SERVERURL + Constant.DATA_UPLOAD + mAnswerid + "_" + (mPageno + 1) + ".jpg";
            IDataManager manager = DataManager.getInstance();
            ReturnData<Bitmap> mReturnData = manager.loadSubmitImage(this, loadPath, mAnswerid);
            mCurBitmap = mReturnData.getData();
            return new Status(mReturnData);
        }
        }
        return super.onTaskInBackground(taskId, bundle);
    }
    
    @Override
    public int onTaskComplete(int taskId, Status status) {
        switch (taskId) {
        case TASK_GET_PIC:{
            mProgressDialog.dismiss();
            Bitmap bmp = Bitmap.createScaledBitmap(mCurBitmap, mImgv.getWidth(), mImgv.getHeight(), false);
            mBitmaps[mPageno] = bmp;
            mImgv.setImageBitmap(mBitmaps[mPageno]);
        }
            break;
        }
        
        return super.onTaskComplete(taskId, status);
    }
    
    private void setButtonStatus(){
        mPrevBtn.setVisibility(View.VISIBLE);
        mNextBtn.setVisibility(View.VISIBLE);
        if(mBitmaps.length == 1){
            mPrevBtn.setVisibility(View.GONE);
            mNextBtn.setVisibility(View.GONE);
        }else {
            if(mPageno == 0){
                mPrevBtn.setVisibility(View.GONE);
            }else if (mPageno == mBitmaps.length - 1){
                mNextBtn.setVisibility(View.GONE);
            }
        }
    }
    
    private class MyOnClickListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.iv_answer_previous:
                previous();
                break;

            case R.id.iv_answer_next:
                next();
                break;
            }
        }

        private void previous(){
            mPageno --;
            setPicImg();
        }
        
        private void next(){
            mPageno ++;
            setPicImg();
        }
        
        private void setPicImg(){
            setButtonStatus();
            mBarCenterTitleTv.setText("第" + (mPageno + 1) + "页，共" + mBitmaps.length + "页");
            if(mBitmaps[mPageno] == null){
                startTask(TASK_GET_PIC);
            }else{
                mImgv.setImageBitmap(mBitmaps[mPageno]);
            }
        }
        
    }
}
